package com.lly.tboc.zhouyi

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults.cardColors
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.lly.tboc.R
import com.lly.tboc.zhouyi.vm.YaoViewModel
import com.lly.ui.GuaView

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun YaoScreen(
    navController: NavController,
    guaId: Int,
    viewModel: YaoViewModel = hiltViewModel()
) {
    viewModel.load(guaId)
    Surface(color = MaterialTheme.colorScheme.primary) {
        val scrollBehavior =
            TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState())
        Scaffold(
            modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
            containerColor = Color.Transparent,
            topBar = {
                CenterAlignedTopAppBar(
                    colors = TopAppBarDefaults.topAppBarColors(
                        containerColor = MaterialTheme.colorScheme.primaryContainer,
                        titleContentColor = MaterialTheme.colorScheme.primary,
                    ),
                    title = {
                        Text(
                            text = stringResource(id = R.string.zhouyi),
                            maxLines = 1,
                            overflow = TextOverflow.Ellipsis
                        )
                    },
                    navigationIcon = {
                        IconButton(onClick = { navController.popBackStack() }) {
                            Icon(
                                imageVector = Icons.AutoMirrored.Filled.ArrowBack,
                                contentDescription = "Localized description"
                            )
                        }
                    },
                    scrollBehavior = scrollBehavior,
                )

            }
        ) {
            YaoContent(viewModel, Modifier.padding(it))
        }
    }
}

@Composable
private fun YaoContent(viewModel: YaoViewModel, modifier: Modifier) {
    val gua = viewModel.gua
    val yaoes = viewModel.yaoes
    val wenYan = viewModel.wenYan

    gua?.apply {
        Column(
            modifier = modifier
                .verticalScroll(rememberScrollState())
        ) {
            Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(8.dp)) {
                Text(text = name, style = MaterialTheme.typography.titleLarge)
                Spacer(modifier = Modifier.size(8.dp))
                val yaoColor = LocalContentColor.current
                GuaView(
                    yaoes = gua.yaoViewData,
                    selectedColor = yaoColor,
                    color = yaoColor,
                    modifier = Modifier
                        .height(30.dp)
                        .width(30.dp)
                )
            }
            yaoes.forEach { yao ->
                var isExpand by remember {
                    mutableStateOf(true)
                }
                Card(
                    modifier = Modifier
                        .fillMaxWidth()
                        .clickable { isExpand = !isExpand }
                        .padding(16.dp),
                    colors = cardColors(containerColor = MaterialTheme.colorScheme.inversePrimary)
                ) {
                    Column(modifier = Modifier.padding(8.dp)) {
                        if (isExpand) {
                            Text(text = yao.name)
                            Text(text = yao.ci)
                            Text(text = yao.xiang ?: "")
                            Text(text = yao.wu ?: "")
                        }
                    }
                }
            }
            wenYan?.wenYan?.let { wenYan ->
                Card(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp),
                    colors = cardColors(containerColor = MaterialTheme.colorScheme.inversePrimary)
                ) {
                    Column(modifier = Modifier.padding(8.dp)) {
                        Text(text = wenYan)
                    }

                }
            }
        }
    }
}
